<?php
require("bases.php");

class student extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "student";
			$this->position_one = array("text"=>"学生管理");
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$obj_student = spClass('students');
		$obj_student_info = spClass('student_info');
		$postdata = $this->spArgs();
		$page = intval(max($postdata['page'], 1));
		$condition = "1 and sys_student.isdel = 0";
		if($searchkeys = $postdata['searchkeys']){
			$condition .= " and (sys_student.nickname like '%{$searchkeys}%' )";
			$this->searchkeys = $searchkeys;
		}
		$data_rs = $obj_student->join('sys_student_info','sys_student.id = sys_student_info.uid','left')
            ->join('sys_classes','sys_classes.id = sys_student_info.grade_id','left')
			->spPager($page, 10)->findAll($condition,  'sys_student.id asc','sys_student.* , 
			sys_student_info.name as student_name, sys_student_info.phone,sys_student_info.classname,sys_classes.name as gradename');
		$this->data_rs = $data_rs;

		$this->pager = $obj_student->spPager()->getPager();
		$this->url = spUrl($this->controller, 'datalist', array("searchkeys"=>$this->searchkeys));
		$this->position_two = array("text"=>"学生管理", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生管理");
		$this->searchform = 1;
	}

	public function create(){
		$obj_student = spClass('students');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			try{
				$postdata = $this->spArgs();
				$data = array();	
				$data['account'] = $postdata['account'];
				$data['password'] = $postdata['password'];
				$data['status'] = intval($postdata['status']);
				if($result = $obj_student->spValidator($data)) {
					foreach($result as $item) {
						throw new Exception($item[0]);
					}
				}
				$data['password'] = md5($data['password']);
				if(!$obj_student->create($data))
					throw new Exception("未知错误，学生添加失败");
				$url = spUrl($this->controller, "datalist");
				$message = array('msg'=>'学生添加成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg'=>$e->getMessage() ,'result'=>-1);
				echo json_encode($message);
				exit();
			}
		}
		$this->saveurl = spUrl($this->controller, "create");
		$this->position_two = array("text"=>"学生添加", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生添加");
	}

	function modify(){
		try {
			$postdata = $this->spArgs();
			$obj_student = spClass("students");
			if(!$id = intval($postdata['id']))
				throw new Exception("非法操作");
			if(!$info_rs = $obj_student->join('sys_student_info','sys_student.id = sys_student_info.uid','left')
                ->join('sys_classes','sys_classes.id = sys_student_info.grade_id','left')
                ->find(array("sys_student.id"=>$id),null,'sys_student.* , 
			sys_student_info.name as student_name, sys_student_info.phone,sys_student_info.classname,sys_classes.name as gradename'))
				throw new Exception("找不到该生信息，可能已被删除");
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$data = array();
					//客户端提交方法
					$data['account'] = $postdata['account'];
					$pwdmodify = 0;
					if($postdata['password']){
						$pwdmodify = 1;
						$data['password'] = $postdata['password'];
					}else{
						$data['password'] = "nomodify";
					}
					$data['status'] = intval($postdata['status']);
					if($result = $obj_student->spValidator($data)) {
						foreach($result as $item) {
							throw new Exception($item[0]);
						}
					}
					if($pwdmodify){
						$data['password'] = md5($data['password']);
					}else{
						unset($data["password"]);
					}
					if(!$obj_student->update(array("id"=>$id), $data))
						throw new Exception("未知错误，学年信息更新失败");
					$url = spUrl($this->controller, "datalist");
					$message = array('msg'=>'修改成功！','result'=>0, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
		$this->id = $id;
		$this->info_rs = $info_rs;
		$this->saveurl = spUrl($this->controller, "modify");
		$this->position_two = array("text"=>"学生信息修改", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生信息修改");
		$this->display($this->controller."/create.html");
	}

	public function delete(){
		$id = $this->spArgs('id');
		$obj_student = spClass('students');
		if($id){
			$url = spUrl($this->controller, "datalist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('id' => $v);
					$obj_student->update($conditions, array('isdel' => 1));
				}
				echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('id' => $id);
				if($obj_student->update($conditions,array('isdel' =>1))){
					echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'删除失败！' , 'result'=>0, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择删除项，再进行操作！' , 'result'=>0));
			exit();
		}
	}

}
?>